﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LigerMVC.Common
{ 
    /// <summary>
    /// 通用的数据检索信息，包括分页、排序、查询条件
    /// </summary>
    public class QueryDescriptor
    {
        public QueryDescriptor()
        {
            Condition = new FilterGroup();
        }
         


        public IList<OrderByClause> OrderBy { get; set; }

        public FilterGroup Condition { get; set; }

        public int PageSize { get; set; }

        public int PageIndex { get; set; }


        /// <summary>
        /// 获取全文搜索条件
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static FilterGroup GetFilterGroup<TEntity>(string key)
        {
            var conditions = new FilterGroup
            {
                rules = new List<FilterRule>(),
                op = "or"
            };
            foreach (var pro in typeof(TEntity).GetProperties())
            {
                if (!pro.CanWrite) continue;
                if (pro.PropertyType != typeof(string)) continue;
                conditions.rules.Add(new FilterRule()
                {
                    field = pro.Name,
                    value = key,
                    op = QueryOperator.Contains.ToString()
                });
            }
            return conditions;
        }
    } 
}
